// pages/class1/class1.js

function bindone(){
  var i;
  var k;
  var app = getApp();
  i = Math.floor(Math.random() * 2);
  if(i == 0 ){//加法
    app.globalData.addend1 = Math.floor(Math.random() * 100);
    k = 100 - app.globalData.addend1;
    app.globalData.addend2 = Math.floor(Math.random() * k);
    app.globalData.answer = (app.globalData.addend1 + app.globalData.addend2);
    app.globalData.question = (app.globalData.addend1 + " + " + app.globalData.addend2 + " = ");
  }
  if(i == 1){//减法
    app.globalData.addend1 = Math.floor(Math.random() * 100);
    app.globalData.addend2 = Math.floor(Math.random() * 100);
    app.globalData.answer = Math.abs(app.globalData.addend1 - app.globalData.addend2);
    if(app.globalData.addend1 >= app.globalData.addend2){
      app.globalData.question = (app.globalData.addend1 + " - " + app.globalData.addend2 + " = ");
    }
    else{
      app.globalData.question = (app.globalData.addend2 + " - " + app.globalData.addend1 + " = ");
    }
  }
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    data_input:{},
    cir:"",
    answer:0,
    remainder : 0,
    modalHidden:true,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '题目 一年级'
    })
    bindone();
    console.log(this.data.answer);
    this.setData({
      answer:getApp().globalData.answer,
      cir:getApp().globalData.question,
      remainder:getApp().globalData.remainder,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  datain: function(e){
    this.setData({
      data_input:e.detail.value,
    });
  },

  submit:function(){
    try{
      getApp().data.yop = wx.getStorageSync('wrong_n')
    }catch(e){
      getApp().data.yop = 0;
    }
    getApp().data.yop++,
    wx.setStorage({
      data: getApp().data.yop,
      key: "wrong_n",
    })
    var num=(getApp().data.yop-1) % 30;
    try{
      getApp().data.data1 = wx.getStorageSync('his')
      if(!getApp().data.data1) getApp().data.data1 = [];
    }catch(e){
    }
    getApp().data.data1[num]=this.data.cir+this.data.answer;
    wx.setStorage({
      data: getApp().data.data1,
      key: "his",
    })
    if(this.data.answer==this.data.data_input){
      var that=this;
      that.onShow();
    } else{
      try{
        getApp().data.xop = wx.getStorageSync('right_n')
      }catch(e){
      }
      getApp().data.xop++;
      wx.setStorage({
        data: getApp().data.xop,
        key: "right_n",
      })
      var num=(getApp().data.xop-1) % 30;
      try{
        getApp().data.data2 = wx.getStorageSync('wro')
        if(!getApp().data.data2) getApp().data.data2 = [];
      }catch(e){
      }
      getApp().data.data2[num]=this.data.cir+this.data.answer;
      wx.setStorage({
        data: getApp().data.data2,
        key: "wro",
      })
      this.setData({
        modalHidden:!this.data.modalHidden
      })
    }
  },

  //确定按钮点击事件
  modalBindaconfirm:function(){
    this.setData({
      modalHidden:!this.data.modalHidden,
    })
    var that=this;
    that.onShow();
  },
  //取消按钮点击事件
  modalBindcancel:function(){
    this.setData({
      modalHidden:!this.data.modalHidden,
    })
  },

})